//
//  MCViewAutoFill.m
//  MirkoLibTest
//
//  Created by Mirko on 2017/3/31.
//  Copyright © 2017年 Mirko. All rights reserved.
//

#import "MCViewAutoFill.h"

@interface MCViewAutoFill ()

-(NSArray *) findValidViewWithRootView:(UIView *) view;
-(NSMutableArray *)listSubviewsOfView:(UIView *) targView rootView:(UIView *) rootView;
-(NSArray *) sortViewList:(NSMutableArray *) views rootView:(UIView *) view;

@end

@implementation MCViewAutoFill


-(instancetype) initWithController:(UIViewController *) controller
{
    self = [super init];
    if (self) {
        self.viewController = controller;
        self.viewList = [[NSMutableArray alloc] init];
        self.autoFillDatas = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void) autoFillWithDatas:(NSArray *) datas
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [self.autoFillDatas addObjectsFromArray:datas];
        NSArray *viewArr = [self findValidViewWithRootView:self.viewController.view];
        [self.viewList addObjectsFromArray:viewArr];
        
        int counter = MIN((int)self.autoFillDatas.count, (int)viewArr.count);
        
        for (int i = 0; i <counter; ++i) {
            UIView *view = viewArr[i];
            NSString *string = self.autoFillDatas[i];
            dispatch_async(dispatch_get_main_queue(), ^{
                [view autoFill:string];
            });
        }
        viewArr = nil;
    });
}

-(NSArray *) findValidViewWithRootView:(UIView *) view;
{
    NSMutableArray *validView = [self listSubviewsOfView:view rootView:view];
    return [self sortViewList:validView rootView:view];
}

#pragma mark member fun
-(NSArray *) sortViewList:(NSMutableArray *) views rootView:(UIView *) view
{
    [views sortUsingComparator:^NSComparisonResult(NSArray *  _Nonnull obj1, NSArray *  _Nonnull obj2) {
        NSLog(@"obj1:%@  obj2:%@", obj1, obj2);
        CGPoint point1 = [obj1[1] CGPointValue];
        CGPoint point2 = [obj2[1] CGPointValue];
        
        if (point1.y < point2.y) {
            return NSOrderedAscending;
        }
        else
        {
            return NSOrderedDescending;
        }
    }];
    
    NSMutableArray *result = [[NSMutableArray alloc] init];
    for (NSArray *item in views) {
        [result addObject:[item firstObject]];
    }
    [views removeAllObjects];
    return result;
}


- (NSMutableArray *)listSubviewsOfView:(UIView *) targView rootView:(UIView *) rootView
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSArray *subviews = [targView subviews];
    if ([subviews count] == 0)
    {
        return arr;
    }
    
    for (UIView *view in subviews) {
        if (view.hidden) {
            continue;
        }
        CGPoint point = [targView convertPoint:view.frame.origin toView:rootView];
        NSLog(@"%@, point: %@", NSStringFromClass([view class]), NSStringFromCGPoint(point));
        if ([view isMemberOfClass:[UIView class]] && view.subviews.count != 0) {
            NSMutableArray *subArr = [self listSubviewsOfView:view rootView:rootView];
            if (subArr.count != 0) {
                [arr addObjectsFromArray:subArr];
                [subArr removeAllObjects];
            }
        }
        else
        {
            NSArray *result = [NSArray arrayWithObjects:view, [NSValue valueWithCGPoint:point], nil];
            [arr addObject:result];
        }
    }
    return arr;
}

-(void) autoFillItem:(NSString *) string index:(NSInteger) viewIdx
{
    if (viewIdx > self.viewList.count - 1) {
        return;
    }
    UIView *view = self.viewList[viewIdx];
    [view autoFill:string];
}

-(void) releasSource
{
    
}





@end
